package com.example.erp.domain;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import com.alibaba.fastjson.annotation.JSONField;
import com.example.erp.exception.StatusDefinition;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.Map;

public class DataResult<T> {
    private static final long serialVersionUID = 3335774490588365954L;
    @JsonProperty("status")
    @JsonInclude(Include.NON_NULL)
    private Status status = new Status("000000", (String) null);
    @JsonProperty("result")
    @JsonInclude(Include.NON_NULL)
    private T result;
    @JsonProperty("attachment")
    @JsonInclude(Include.NON_NULL)
    private Map<String, Object> attachment;

    public DataResult() {
    }

    public static DataResult<Object> success(Object result) {
        DataResult<Object> dataResult = new DataResult();
        dataResult.setResult(result);
        return dataResult;
    }

    public static <T> DataResult<T> success(Object result, Class<T> type) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setResult((T) result);
        return dataResult;
    }

    public static <T> DataResult<T> success() {
        return new DataResult();
    }

    public static <T> DataResult<T> success(String msg) {
        DataResult<T> dataResult = new DataResult();
        dataResult.getStatus().setStatusReason(msg);
        return dataResult;
    }

    public static <T> DataResult<T> successObj(T t) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setResult(t);
        return dataResult;
    }

    public static <T> DataResult<T> successList(T t) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setResult(t);
        return dataResult;
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static <T> DataResult<T> faild(String errorCode, String resultCode, String errMsg) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setStatus(new Status(errorCode, errMsg));
        return dataResult;
    }

    public static <T> DataResult<T> faild(String errorCode, String errMsg) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setStatus(new Status(errorCode, errMsg));
        return dataResult;
    }

    public static <T> DataResult<T> rpcFaild(String errorCode, String errMsg) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setStatus(new Status(errorCode, errMsg));
        return dataResult;
    }

    public static <T> DataResult<T> faild(StatusDefinition statusDefinition) {
        DataResult<T> dataResult = new DataResult();
        dataResult.setStatus(new Status(statusDefinition.getErrorCode(), statusDefinition.getErrorReason()));
        return dataResult;
    }

    public static DataResult<Map<String, Object>> make(String key, Object value) {
        DataResult<Map<String, Object>> dataResult = new DataResult();
        if (null == dataResult.getResult()) {
            dataResult.setResult(new HashMap(1));
        }

        ((Map) dataResult.result).put(key, value);
        return dataResult;
    }

    public DataResult<T> addResult(String key, Object value) {
        if (this.getResult() instanceof Map) {
            Map<String, Object> result = (Map) this.getResult();
            result.put(key, value);
        }

        return this;
    }

    public Status getStatus() {
        return this.status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public T getResult() {
        return this.result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public boolean isSuccess() {
        return null != this.status && "000000".equals(this.getStatus().getStatusCode());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public boolean isFailed() {
        return !this.isSuccess();
    }

    public Map<String, Object> getAttachment() {
        return this.attachment;
    }

    public void setAttachment(Map<String, Object> attachment) {
        this.attachment = attachment;
    }

    public void addAttachment(String key, Object value) {
        if (this.attachment == null) {
            this.attachment = new HashMap(1);
        }

        this.attachment.put(key, value);
    }
}
